package com.qf.boot2302.controller;

import cn.hutool.core.io.FileUtil;
import com.qf.boot2302.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/up")
@Slf4j
public class UploadController {

    @Value("${qf.fileupload.path}")
    private String picdir;

    @RequestMapping("/pic")
    public ResultVo upload( MultipartFile photo, MultipartFile pic) throws IOException {
        log.info("接收到文件为:{},{}",photo.getOriginalFilename(),pic.getOriginalFilename());
//        String extname = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf("."));
        String extName = FileUtil.extName(photo.getOriginalFilename());
//        String picdir = "D:\\pic2302";
        String filename = UUID.randomUUID().toString().replaceAll("-", "")+"."+extName;
        String filepath = picdir+"\\"+ filename;
        // 存放文件
        photo.transferTo(new File(filepath));

        return ResultVo.ok(1, "ok", filename);
    }

}
